package repository

import (
	"github.com/honwee/awm/internal/model"
	"github.com/honwee/awm/pkg/database"
	"gorm.io/gorm"
)

type TraceRepository struct {
	db *gorm.DB
}

func NewTraceRepository() *TraceRepository {
	return &TraceRepository{
		db: database.DB,
	}
}

// Create 创建追踪日志
func (r *TraceRepository) Create(trace *model.Trace) error {
	return r.db.Create(trace).Error
}

// FindByWorkflowID 根据工作流ID查询追踪日志
func (r *TraceRepository) FindByWorkflowID(workflowID string, offset, limit int) ([]model.Trace, int64, error) {
	var traces []model.Trace
	var total int64

	// 查询总数
	if err := r.db.Model(&model.Trace{}).Where("workflow_id = ?", workflowID).Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 查询数据
	err := r.db.Where("workflow_id = ?", workflowID).
		Offset(offset).
		Limit(limit).
		Order("created_at DESC").
		Find(&traces).Error

	return traces, total, err
}

// FindByWorkflowRunID 根据工作流运行ID查询追踪日志
func (r *TraceRepository) FindByWorkflowRunID(workflowRunID string, offset, limit int) ([]model.Trace, int64, error) {
	var traces []model.Trace
	var total int64

	// 查询总数
	if err := r.db.Model(&model.Trace{}).Where("workflow_run_id = ?", workflowRunID).Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 查询数据
	err := r.db.Where("workflow_run_id = ?", workflowRunID).
		Offset(offset).
		Limit(limit).
		Order("created_at DESC").
		Find(&traces).Error

	return traces, total, err
}

// FindByNodeRunID 根据节点运行ID查询追踪日志
func (r *TraceRepository) FindByNodeRunID(nodeRunID string, offset, limit int) ([]model.Trace, int64, error) {
	var traces []model.Trace
	var total int64

	// 查询总数
	if err := r.db.Model(&model.Trace{}).Where("node_run_id = ?", nodeRunID).Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 查询数据
	err := r.db.Where("node_run_id = ?", nodeRunID).
		Offset(offset).
		Limit(limit).
		Order("created_at DESC").
		Find(&traces).Error

	return traces, total, err
}

// FindByNodeID 根据节点ID查询追踪日志
func (r *TraceRepository) FindByNodeID(nodeID string, offset, limit int) ([]model.Trace, int64, error) {
	var traces []model.Trace
	var total int64

	// 查询总数
	if err := r.db.Model(&model.Trace{}).Where("node_id = ?", nodeID).Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 查询数据
	err := r.db.Where("node_id = ?", nodeID).
		Offset(offset).
		Limit(limit).
		Order("created_at DESC").
		Find(&traces).Error

	return traces, total, err
}

// FindByEvent 根据事件类型和其他条件查询追踪日志
func (r *TraceRepository) FindByEvent(event, workflowID, workflowRunID, nodeID string, offset, limit int) ([]model.Trace, int64, error) {
	var traces []model.Trace
	var total int64

	query := r.db.Model(&model.Trace{}).Where("event = ?", event)

	// 添加可选的过滤条件
	if workflowID != "" {
		query = query.Where("workflow_id = ?", workflowID)
	}
	if workflowRunID != "" {
		query = query.Where("workflow_run_id = ?", workflowRunID)
	}
	if nodeID != "" {
		query = query.Where("node_id = ?", nodeID)
	}

	// 查询总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 查询数据
	err := query.Offset(offset).
		Limit(limit).
		Order("created_at DESC").
		Find(&traces).Error

	return traces, total, err
}
